package com.guanghua.brick.html;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TreeControl extends WebControl {
	
	private static final String ROOT_PK = "##_root_##";
	
	private String textProperty = null;
	private String treeNodeFunction = null;
	private String primaryKeyProperty = "primaryKey";
	private String parentKeyProperty = "parentKey";
	private String url = null;
	private String method = "POST";
	private boolean lazy = false;
	
	@Override
	public String buildHTML(HttpServletRequest request, HttpServletResponse response) {
		StringBuffer html = new StringBuffer();
		//生成tree contain
		html.append("<div id=\"")
		.append(this.getId())
		.append(".contain\" ");
	
		//如果有url则通过url加载content
		if (this.url != null)
			html.append("url=\"")
				.append(this.url)
				.append("\" ");
		//传入content id
		if (this.getContent() != null)
			html.append("content=\"")
			.append(this.getContent().getId())
			.append("\" ");
		
		if (this.lazy)
			html.append("lazyLoading ");
		
		html.append("method=\"")
			.append(this.method)
			.append("\" primaryKeyProperty=\"")
			.append(this.primaryKeyProperty)
			.append("\" parentKeyProperty=\"")
			.append(this.parentKeyProperty)
			.append("\" ");
		
		if (this.textProperty != null)
			html.append("textProperty=\"")
				.append(this.textProperty)
				.append("\" ");
		
		html.append(this.getHtml())
			.append("></div>\n");
		
		
		//scripts生成
		html.append("<script language=\"javascript\">\n");
		html.append("	var ")
			.append(this.getId())
			.append(" = new Tree(\"")
			.append(this.getId())
			.append("\");\n");
		
		if (treeNodeFunction != null)
			html.append("	").append(this.getId()).append(".onContentBuild = ").append(treeNodeFunction).append(";\n");
		
		html.append("	").append(this.getId()).append(".fetchContent(").append((this.lazy)?"\""+ROOT_PK+"\"":"").append(");\n");
		html.append("	").append(this.getId()).append(".create();\n");
		
		//生成depend
		if (this.getDepends() != null) {
			Iterator<IDepend> i = this.getDepends().iterator();
			while (i.hasNext()) {
				html.append("	")
				.append(i.next().buildScripts(this.getId()))
				.append("\n");
			}
		}
		
		html.append("</script>");
		
		return html.toString();
	}

	public boolean isLazy() {
		return lazy;
	}

	public void setLazy(boolean lazy) {
		this.lazy = lazy;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getParentKeyProperty() {
		return parentKeyProperty;
	}

	public void setParentKeyProperty(String parentKeyProperty) {
		this.parentKeyProperty = parentKeyProperty;
	}

	public String getPrimaryKeyProperty() {
		return primaryKeyProperty;
	}

	public void setPrimaryKeyProperty(String primaryKeyProperty) {
		this.primaryKeyProperty = primaryKeyProperty;
	}

	public String getTextProperty() {
		return textProperty;
	}

	public void setTextProperty(String textProperty) {
		this.textProperty = textProperty;
	}

	public String getTreeNodeFunction() {
		return treeNodeFunction;
	}

	public void setTreeNodeFunction(String treeNodeFunction) {
		this.treeNodeFunction = treeNodeFunction;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	
	
	
}
